// 设备管理API - 对接后端接口
import { get, post, put, del } from './request.js'

// 设备档案管理
// 获取设备列表（分页查询）
export const getDeviceList = (params) => {
  return get('/api/devices', params)
}

// 获取设备详情
export const getDeviceDetail = (deviceId) => {
  return get(`/api/devices/${deviceId}`)
}

// 创建设备
export const createDevice = (data) => {
  return post('/api/devices', data)
}

// 更新设备
export const updateDevice = (data) => {
  return put('/api/devices', data)
}

// 删除设备
export const deleteDevice = (deviceId) => {
  return del(`/api/devices/${deviceId}`)
}

// 设备状态管理
// 查询设备是否在线
export const getDeviceOnline = (deviceId, timeoutMillis) => {
  return get(`/api/devices/${deviceId}/online`, { timeoutMillis })
}

// 获取设备状态日志
export const getDeviceStatusLogs = (deviceId, pageNum, pageSize) => {
  return get(`/api/devices/${deviceId}/status-logs`, { pageNum, pageSize })
}

// 设备分组管理
// 获取分组树
export const getDeviceGroups = () => {
  return get('/api/device-groups/tree')
}

// 创建/更新分组
export const saveDeviceGroup = (data) => {
  return post('/api/device-groups', data)
}

// 删除分组
export const deleteDeviceGroup = (groupId) => {
  return del(`/api/device-groups/${groupId}`)
}

// 绑定设备到分组
export const bindDevicesToGroup = (groupId, deviceIds) => {
  return put(`/api/device-groups/${groupId}/devices`, deviceIds)
}

// 设备配置管理
// 获取设备配置
export const getDeviceConfig = (deviceId) => {
  return get(`/api/devices/${deviceId}/configs`)
}

// 更新设备配置
export const updateDeviceConfig = (deviceId, data) => {
  return put(`/api/devices/${deviceId}/configs`, data)
}

// 设备心跳上报
export const reportHeartbeat = (deviceId, ttlMinutes = 45) => {
  return post(`/api/devices/${deviceId}/heartbeat`, null, { ttlMinutes })
}

// 维护计划管理
// 获取维护计划
export const getMaintenancePlans = (params) => {
  return get('/api/maintenance/plans', params)
}

// 创建维护计划
export const createMaintenancePlan = (data) => {
  return post('/api/maintenance/plans', data)
}

// 删除维护计划
export const deleteMaintenancePlan = (planId) => {
  return del(`/api/maintenance/plans/${planId}`)
}

// 更新维护计划状态
export const updateMaintenancePlanStatus = (planId, status) => {
  return put(`/api/maintenance/plans/${planId}/status`, null, { status })
}

// 维护记录管理
// 获取维护记录
export const getMaintenanceRecords = (params) => {
  return get('/api/maintenance/records', params)
}

// 创建维护记录
export const createMaintenanceRecord = (data) => {
  return post('/api/maintenance/records', data)
}

// 测试设备连接
export const testDeviceConnection = (deviceId) => {
  return post(`/api/devices/${deviceId}/test-connection`)
}

// 地图设备相关API
// 获取地图设备列表（包含经纬度信息）
export const getMapDeviceList = () => {
  return get('/device/list')
}

// 获取设备最新水质数据
export const getDeviceLatestData = (deviceId) => {
  return get('/device/latest', { deviceId })
}

// 获取设备详细信息
export const getDeviceInfo = (deviceId) => {
  return get('/device/info', { deviceId })
}